﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SQLite;
using Com.Toook.Common;

public partial class UserControls_JokeRecommendControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitRecommend();
    }
    /// <summary>
    /// 随机推荐同类笑话
    /// </summary>
    private void InitRecommend()
    {
        int type = ParamHelper.ParseIntPositive(Request["type"]);       
        string typeCondition = "";
        //类型不为空且是数字，设置类型条件。否则不加类型条件。
        if (type > 0)
        {
            typeCondition = " where type=" + type;
        }        

        DataRow row = PageData.SelectParamRow("typeCode='joke" + type.ToString() + "'");
        int total = 1;
        if (row == null)//没有找到，类型不对.设置为默认
        {
            row = PageData.SelectParamRow("typeCode='joke'");
        }
        int showCount = 15;//随机推荐显示数
        total = int.Parse(row[PageColumn.totalCount].ToString());
        int start = 0;
        if (total > showCount)
        {
            Random rand = new Random(DateTime.Now.Second + 593);
            start = rand.Next(total - showCount);
        }
        
        string sql = "select id, title from JokePublish " + typeCondition +
            " limit " + start.ToString() + "," + showCount.ToString();
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader reader = null;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            rptRecommend.DataSource = reader;
            rptRecommend.DataBind();
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
    }
}
